{$mode delphi}

{
  $Id: commonfunc.pas 4 2006-10-05 09:18:53Z kcynarski $
  $HeadURL: http://isbuilder.googlecode.com/svn/branches/unify_modules/commonfunc.pas $
  Inno Setup Builder
  Common functions
}

unit commonfunc;

interface

uses sysutils, DOM;

{-- Constants --}

const
  vlNormal  = 1;
  vlDebug   = 2;

{-- XML Helper Funtions --}

function XMLGetAttr(node: TDOMNode; name, default: string):string;
procedure XMLSetAttr(node: TDOMNode; name, value:string);

{-- Debug Helper Functions --}

procedure SetDebugVerboseLevel(level : byte);
procedure Notify(s:string); overload;
procedure Notify(s:string; args: array of const); overload;
procedure Debug(s:string); overload;
procedure Debug(s:string; args: array of const); overload;
procedure Warning(s:string); overload;
procedure Warning(s:string; args: array of const); overload;
procedure FatalError(s:string); overload;
procedure FatalError(s:string; e:Exception); overload;
procedure FatalError(s:string; args: array of const); overload;
procedure FatalError(s:string; args: array of const; e:Exception); overload;

implementation

var DebugVerboseLevel: byte;

procedure SetDebugVerboseLevel(level: byte);
begin
  DebugVerboseLevel := level;
end;

function XMLGetAttr(node: TDOMNode; name, default: string):string;
var i: integer;
    attr : TDOMNode;
begin
  result := default;
  if (node.Attributes <> nil) and (node.Attributes.Length > 0) then begin
    for i := 0 to node.Attributes.Length - 1 do begin
      attr := node.Attributes.Item[i];
      if upcase(attr.nodename) = upcase(name) then 
         result := attr.nodevalue;
    end;
  end;
end;

procedure XMLSetAttr(node: TDOMNode; name, value:string);
var i: integer;
    attr : TDOMNode;
begin
  if (node.Attributes <> nil) and (node.Attributes.Length > 0) then begin
    for i := 0 to node.Attributes.Length - 1 do begin
      attr := node.Attributes.Item[i];
      if upcase(attr.nodename) = upcase(name) then 
         attr.nodevalue:=value;
    end;
  end;
end;  

procedure Notify(s:string); overload;
begin
  writeln(' [ ] '+s);
end;

procedure Notify(s:string; args: array of const); overload;
begin
  Notify(Format(s,args));
end;

procedure Debug(s:string); overload;
begin
  if DebugVerboseLevel >= vlDebug then
     writeln(' [%] '+s);
end;

procedure Debug(s:string; args: array of const); overload;
begin
  Debug(Format(s,args));
end;

procedure Warning(s:string); overload;
begin
  writeln(' [!] '+s);
end;

procedure Warning(s:string; args: array of const); overload;
begin
  Warning(Format(s,args));
end;

procedure FatalError(s:string); overload;
begin
  writeln(' [!] Fatal Error: '+s);
end;

procedure FatalError(s:string; e:Exception); overload;
begin
  FatalError(s+#13#10+'     '+e.Message);
end;

procedure FatalError(s:string; args: array of const); overload;
begin
  FatalError(Format(s,args));
end;

procedure FatalError(s:string; args: array of const; e:Exception); overload;
begin
  FatalError(Format(s+#13#10+'     '+e.Message,args));
end;

begin
  DebugVerboseLevel := vlNormal; // Set by default normal verbose mode
end.